<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="状态" prop="status" >
        <a-select placeholder="请选择状态" v-model="form.status">
          <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="删除标志" prop="delFlag" v-if="formType === 1">
        <a-input v-model="form.delFlag" placeholder="请输入删除标志" />
      </a-form-model-item>
      <a-form-model-item label="昵称" prop="nickName" >
        <a-input v-model="form.nickName" placeholder="请输入昵称" />
      </a-form-model-item>
      <a-form-model-item label="用户手机号" prop="userPhone" >
        <a-input v-model="form.userPhone" placeholder="请输入用户手机号" />
      </a-form-model-item>
      <a-form-model-item label="密码" prop="password" >
        <a-input v-model="form.password" placeholder="请输入密码" />
      </a-form-model-item>
      <a-form-model-item label="邀请人id" prop="superId" >
        <a-input v-model="form.superId" placeholder="请输入邀请人id" />
      </a-form-model-item>
      <a-form-model-item label="邀请码" prop="inviteCode" >
        <a-input v-model="form.inviteCode" placeholder="请输入邀请码" />
      </a-form-model-item>
      <a-form-model-item label="直推人数" prop="pushNum" >
        <a-input v-model="form.pushNum" placeholder="请输入直推人数" />
      </a-form-model-item>
      <a-form-model-item label="团队人数" prop="teamNum" >
        <a-input v-model="form.teamNum" placeholder="请输入团队人数" />
      </a-form-model-item>
      <a-form-model-item label="银行卡账号开户名" prop="bankInfo" >
        <a-input v-model="form.bankInfo" placeholder="请输入银行卡账号开户名" />
      </a-form-model-item>
      <a-form-model-item label="银行卡号" prop="bankNo" >
        <a-input v-model="form.bankNo" placeholder="请输入银行卡号" />
      </a-form-model-item>
      <a-form-model-item label="开户银行" prop="bankName" >
        <a-input v-model="form.bankName" placeholder="请输入开户银行" />
      </a-form-model-item>
      <a-form-model-item label="收货地址" prop="address" >
        <a-input v-model="form.address" placeholder="请输入收货地址" />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark" >
        <a-input v-model="form.remark" placeholder="请输入备注" />
      </a-form-model-item>
      <a-form-model-item label="用户类型" prop="userType" >
        <a-select placeholder="请选择用户类型" v-model="form.userType">
          <a-select-option v-for="(d, index) in userTypeOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="约/抢单权限" prop="powerStatus" >
        <a-select placeholder="请选择约/抢单权限" v-model="form.powerStatus">
          <a-select-option v-for="(d, index) in powerStatusOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="分组一权限" prop="groupOnePower" >
        <a-select placeholder="请选择分组一权限" v-model="form.groupOnePower">
          <a-select-option v-for="(d, index) in groupOnePowerOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="分组二权限" prop="groupTwoPower" >
        <a-select placeholder="请选择分组二权限" v-model="form.groupTwoPower">
          <a-select-option v-for="(d, index) in groupTwoPowerOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="分组三权限" prop="groupThreePower" >
        <a-select placeholder="请选择分组三权限" v-model="form.groupThreePower">
          <a-select-option v-for="(d, index) in groupThreePowerOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="自动预约状态" prop="autoStatus" >
        <a-select placeholder="请选择自动预约状态" v-model="form.autoStatus">
          <a-select-option v-for="(d, index) in autoStatusOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="总金额约单状态" prop="allAmountStatus" >
        <a-select placeholder="请选择总金额约单状态" v-model="form.allAmountStatus">
          <a-select-option v-for="(d, index) in allAmountStatusOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="总金额度" prop="amountQuota" >
        <a-input v-model="form.amountQuota" placeholder="请输入总金额度" />
      </a-form-model-item>
      <a-form-model-item label="用户单独公告" prop="userNotice" >
        <a-input v-model="form.userNotice" placeholder="请输入用户单独公告" />
      </a-form-model-item>
      <a-form-model-item label="最大提现额度" prop="maxWithdrawal" >
        <a-input v-model="form.maxWithdrawal" placeholder="请输入最大提现额度" />
      </a-form-model-item>
      <a-form-model-item label="赠送价值" prop="giftValue" >
        <a-input v-model="form.giftValue" placeholder="请输入赠送价值" />
      </a-form-model-item>
      <a-form-model-item label="归属店铺" prop="ascriptionStore" >
        <a-input v-model="form.ascriptionStore" placeholder="请输入归属店铺" />
      </a-form-model-item>
      <a-form-model-item label="店铺名称" prop="storeName" >
        <a-input v-model="form.storeName" placeholder="请输入店铺名称" />
      </a-form-model-item>
      <a-form-model-item label="店长名称" prop="storeLeader" >
        <a-input v-model="form.storeLeader" placeholder="请输入店长名称" />
      </a-form-model-item>
      <a-form-model-item label="店长手机号" prop="storeLeaderPhone" >
        <a-input v-model="form.storeLeaderPhone" placeholder="请输入店长手机号" />
      </a-form-model-item>
      <a-form-model-item label="店长id" prop="storeLeaderId" >
        <a-input v-model="form.storeLeaderId" placeholder="请输入店长id" />
      </a-form-model-item>
      <a-form-model-item label="店铺图片" prop="storeLeaderImg" >
        <a-input v-model="form.storeLeaderImg" placeholder="请输入店铺图片" />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getQdUser, addQdUser, updateQdUser } from '@/api/system/qdUser'

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    },
    userTypeOptions: {
      type: Array,
      required: true
    },
    powerStatusOptions: {
      type: Array,
      required: true
    },
    groupOnePowerOptions: {
      type: Array,
      required: true
    },
    groupTwoPowerOptions: {
      type: Array,
      required: true
    },
    groupThreePowerOptions: {
      type: Array,
      required: true
    },
    autoStatusOptions: {
      type: Array,
      required: true
    },
    allAmountStatusOptions: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        status: null,

        delFlag: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        nickName: null,

        userPhone: null,

        password: null,

        superId: null,

        inviteCode: null,

        pushNum: null,

        teamNum: null,

        bankInfo: null,

        bankNo: null,

        bankName: null,

        address: null,

        remark: null,

        userType: null,

        powerStatus: null,

        groupOnePower: null,

        groupTwoPower: null,

        groupThreePower: null,

        autoStatus: null,

        allAmountStatus: null,

        amountQuota: null,

        userNotice: null,

        maxWithdrawal: null,

        giftValue: null,

        ascriptionStore: null,

        storeName: null,

        storeLeader: null,

        storeLeaderPhone: null,

        storeLeaderId: null,

        storeLeaderImg: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        status: null,

        delFlag: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        nickName: null,

        userPhone: null,

        password: null,

        superId: null,

        inviteCode: null,

        pushNum: null,

        teamNum: null,

        bankInfo: null,

        bankNo: null,

        bankName: null,

        address: null,

        remark: null,

        userType: null,

        powerStatus: null,

        groupOnePower: null,

        groupTwoPower: null,

        groupThreePower: null,

        autoStatus: null,

        allAmountStatus: null,

        amountQuota: null,

        userNotice: null,

        maxWithdrawal: null,

        giftValue: null,

        ascriptionStore: null,

        storeName: null,

        storeLeader: null,

        storeLeaderPhone: null,

        storeLeaderId: null,

        storeLeaderImg: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getQdUser(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateQdUser(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addQdUser(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
